import GameMgr from "./gameMgr";

interface myNode extends cc.Node {
    index?: number
    coverArr?: any[]
}
const { ccclass, property } = cc._decorator;

@ccclass
export default class layoutBtnEvent extends cc.Component {

    collection_node: cc.Node = null

    onLoad() {
        let collection = cc.find("Canvas/bgc/collection")
        this.collection_node = collection
        GameMgr.instance._collection = collection
    }
    onItemClick(event: any, index: number) {
        if (GameMgr.instance.hasClickedArr.filter(item => item === index).length === 0) {
            GameMgr.instance.hasClickedArr.push(index)
            this.calcPosInStore(index)
        }
    }

    calcPosInStore(index: number) {
        let children = this.collection_node.children
        let tempNode: myNode = cc.instantiate(GameMgr.instance.renderCardArr[index])
        tempNode.setPosition(new cc.Vec2(-180 + children.length * (90), 0)) // 相对父节点的位置
        if (!this.clearCommonEle(tempNode)) return
        GameMgr.instance.renderCardArr[index].active = false
        GameMgr.instance.utils.verifyCanRemoveMask()
    }

    /**
     * 游戏规则定义
     * @returns 
     */
    clearCommonEle(tempNode) {
        let allNodes = this.collection_node.children
        if (allNodes && allNodes.length) {
            let commomNode = allNodes.filter(item => item.name === tempNode.name)
            if (commomNode.length === 2) {
                GameMgr.instance.musicMgr.playEffect("success")
                commomNode.forEach(item => {
                    item.removeFromParent(false);
                })
                // 判断是否游戏通过
                if (allNodes.length === 0 && GameMgr.instance.totalCardNum === GameMgr.instance.hasClickedArr.length) {
                    console.log("游戏通过");
                    if(GameMgr.instance.currentLevel===1){
                        GameMgr.instance.utils.showToast(this.node,"恭喜过关","准备好迎接下一关的挑战了吗？","下一关",() => {
                            GameMgr.instance.nextLevel()
                        })
                    }else{
                        GameMgr.instance.utils.showToast(this.node,"恭喜过关","太棒了，恭喜过关！","重新开始",() => {
                            GameMgr.instance.currentLevel = 1
                            GameMgr.instance.reStartGame()
                        })
                    }
                    return true
                }
                return true
            }
            // 存在一个注意排序 最后判断是否六个了 六个了失败
            else if (commomNode.length === 1) {
                console.log("存在一个");
                this.orderByTypeAndInsert(tempNode)
                this.loseGame()
                return true
            }
            // 相同类型的一个也没有 最后判断是否六个了 六个了失败
            else {
                this.collection_node.addChild(tempNode)
                this.loseGame()
                return true
            }
        } else {
            this.collection_node.addChild(tempNode)
            return true
        }
    }
    orderByTypeAndInsert(tempNode) {
        this.collection_node.children.forEach((item, index: any) => {
            if (item.name === tempNode.name) {
                this.collection_node.insertChild(tempNode, index)
                return
            }
        })
    }
    // 游戏失败了
    loseGame() {
        if (this.collection_node.children.length >= 7) {
            console.log("游戏失败了～");
            GameMgr.instance.musicMgr.playEffect("boom")
            GameMgr.instance.utils.showToast(this.node,"游戏失败","前方路灯不亮,试着换个方向","重新开始",() => {
                GameMgr.instance.reStartGame()
            })
            return false
        }
        return true
    }

    start() {

    }

    // update (dt) {}
}
